﻿package com.ww.scm.web;

import static org.jmesa.limit.ExportType.CSV;
import static org.jmesa.limit.ExportType.JEXCEL;
import static org.jmesa.limit.ExportType.PDFP;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jmesa.core.filter.DateFilterMatcher;
import org.jmesa.core.filter.MatcherKey;
import org.jmesa.model.TableModel;
import org.jmesa.view.editor.CellEditor;
import org.jmesa.view.editor.DateCellEditor;
import org.jmesa.view.html.HtmlBuilder;
import org.jmesa.view.html.component.HtmlColumn;
import org.jmesa.view.html.component.HtmlRow;
import org.jmesa.view.html.component.HtmlTable;
import org.jmesa.view.html.editor.HtmlCellEditor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.ww.core.test.utils.DataUtils;
import com.ww.core.utils.DateUtils;
import com.ww.core.utils.IdUtils;
import com.ww.scm.entity.mat.Bill;
import com.ww.scm.service.StorageManager;


@Controller
@RequestMapping(value = "/test")
public class Test {
	
	private StorageManager storageManager;
	
	@Autowired
	public void setStorageManager(StorageManager storageManager) {
		this.storageManager = storageManager;
	}
	
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public String delte(@PathVariable("id") String id) {
		System.out.println("delte id::" + id);
		return "id";
	}
	
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public ModelAndView get(@PathVariable String id) {
		
		List<User> userList=new ArrayList<User>();
		
		for (int i = 0; i < 20; i++) {
			User um = new User();
			um.setId(IdUtils.uuid());
			um.setName("liuwei"+i);
			um.setSex("1");
			
			Role role =new Role(IdUtils.uuid(),"liuwei"+i+"Role");
			um.setRole(role);
			
			List<Pots> potsList=new ArrayList<Pots>();
			for (int j = 0; j < 10; j++) {
				Pots pots=new Pots(IdUtils.uuid(),"pots"+i);
				potsList.add(pots);
			}
			um.setPosts(potsList);
			userList.add(um);
		}

		return new ModelAndView("/welcome","userInfo",userList);  
	}


	@RequestMapping(params = "method=list")
	public ModelAndView list() {
		HashMap<String, String> model = new HashMap<String, String>();
		model.put("name", "liuwei");
		
		
//		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
//		//设置默认排序方式
//		if (!page.isOrderBySetted()) {
//			page.setOrderBy("id");
//			page.setOrder(Page.ASC);
//		}
//		page = storageManager.findBill(page, filters);
		
		
		return new ModelAndView("/welcome", model);
	}
	
	@RequestMapping(value = "/table", method = RequestMethod.GET)
	public ModelAndView table(HttpServletRequest request,HttpServletResponse response) {
		HashMap<String, String> model = new HashMap<String, String>();
		model.put("name", "liuwei");
		
		
//		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
//		//设置默认排序方式
//		if (!page.isOrderBySetted()) {
//			page.setOrderBy("id");
//			page.setOrder(Page.ASC);
//		}
//		page = storageManager.findBill(page, filters);
		List<Bill> billList=new ArrayList<Bill>();
		for (int i = 0; i < 20; i++) {
			Date now=new Date();
			Bill bill=new Bill();
			bill.setId(IdUtils.uuid());
			bill.setBillCode("testCode"+DataUtils.randomId());
			bill.setBillDate(DateUtils.formatDateHms(now));
			bill.setFillTime(DateUtils.formatDateHms(now));
			bill.setOperator("1001");
			billList.add(bill);
			
		}

        String id="user_table";
        TableModel tableModel = new TableModel(id, request, response);
        
        tableModel.setEditable(true);
        
        
        tableModel.setExportTypes(CSV, JEXCEL, PDFP);
        tableModel.setItems(billList);

        HtmlTable htmlTable = new HtmlTable();

        HtmlRow htmlRow = new HtmlRow();
        
        htmlRow.setUniqueProperty("id"); 
        htmlTable.setRow(htmlRow);

        HtmlColumn firstName = new HtmlColumn("id").title("单据主键");
        firstName.setCellEditor(new CellEditor() {
            public Object getValue(Object item, String property, int rowcount) {
                Object value = new HtmlCellEditor().getValue(item, property, rowcount);
                HtmlBuilder html = new HtmlBuilder();
                html.a().href().quote().append("http://www.tom.com/").quote().close();
                html.append(value);
                html.aEnd();
                return html.toString();
            }
        });
        htmlRow.addColumn(firstName);

        HtmlColumn lastName = new HtmlColumn("billCode").title("单据编号");
        htmlRow.addColumn(lastName);

        HtmlColumn term = new HtmlColumn("operator").title("操作人");
        htmlRow.addColumn(term);

        HtmlColumn career = new HtmlColumn("billDate").title("单据日期").cellEditor(new DateCellEditor("yyyy/MM/dd"));;
        htmlRow.addColumn(career);

        tableModel.addFilterMatcher(new MatcherKey(Date.class, "billDate"), new DateFilterMatcher("yyyy/MM/dd"));
        
        tableModel.setTable(htmlTable);

        String html = tableModel.render();
        
		return new ModelAndView("/table", "tableModel",html);
	}
	
	@RequestMapping(value = "/save", method = RequestMethod.GET)
	public String save() {
		//Bill bill=new Bill();
		//bill.setCode(IdUtils.uuid());
		//bill.setBillDate(new Date(0));
		//bill.setBillType("1");
		//storageManager.saveBill(bill);
		
		return "redirect:/test/list";
	}

	@RequestMapping(params = "method=listBoardTopic")
	public String listBoardTopic(String topicId, String userId) {
		System.out.println("topicId:" + topicId);
		System.out.println("user:" + userId);
		System.out.println("call listBoardTopic method.");
		return "listTopic";
	}
	




	
}
